﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace async
{
    class Program
    {

        static void Main(string[] args)
        {
            Test1();


            Console.ReadKey();
        }


        public static async void Test1()
        {

            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("1当前线程ID：{0}", thread_id));


            await Task.Run(() =>
            {
                for (int i = 0; i < 100000000; i++)
                {

                }
                thread_id = Environment.CurrentManagedThreadId.ToString();
                Console.WriteLine(string.Format("2当前线程ID：{0}", thread_id));
            });

            thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("3当前线程ID：{0}", thread_id));

        }




        public static void Test2()
        {

            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("①当前线程ID：{0}", thread_id));

            Task<string> task = Async0();
            string result = task.Result;
            Console.WriteLine(result);

        }




        /// <summary>
        /// 一个异步任务
        /// </summary>
        /// <returns></returns>
        public static async Task<string> Async0()
        {
            // await 之前的代码是在主线程之中运行
            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("②当前线程ID：{0}", thread_id));
            
            // await 标记之后的代码是在子线程中运行
            return await Task.Run(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("Test()方法里面线程ID: {0}", Environment.CurrentManagedThreadId);
                Console.WriteLine("执行时间：" + DateTime.Now.ToString());
                return Environment.CurrentManagedThreadId.ToString();
            });

            // 这个下面的代码没有执行
            // string thread_id2 = Thread.CurrentThread.ManagedThreadId.ToString();
            // Console.WriteLine(string.Format("③当前线程ID：{0}", thread_id2));
        }

        public static int Async1()
        {
            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("⑤/④当前线程ID：{0}", thread_id));
            int sum = 0;
            for (int i = 0; i < 999; i++)
            {
                sum += i;
            }
            return sum;
        }


        public static async Task<int> Async2()
        {
            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("③当前线程ID：{0}", thread_id));
            int result = await Task.Run(new Func<int>(Async1));// await标记之后，代码在子线程中执行。
            thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("⑥当前线程ID：{0}，result：{1}", thread_id, result));
            return result;
        }

        public static async Task<int> Async3()
        {
            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("②当前线程ID：{0}", thread_id));
            int result = await Async2();
            thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("⑦当前线程ID：{0}，result：{1}", thread_id, result));
            return result;
        }

    }
}
